<template>
  <div class="home">
    <transition name="fade1">
      <div v-if="$route.path=='/home'">
        <van-search
          disabled
          v-model="value"
          shape="round"
          placeholder="请输入搜索关键词"
          @click="openPopup"
        />
        <van-swipe class="my-swipe" :autoplay="5000">
          <van-swipe-item v-for="(item) in banner" :key="item.id">
            <img :src="item.image_url" width="100%" style="display: block;" alt />
          </van-swipe-item>
        </van-swipe>
        <channel :channel="channel"></channel>
        <Brand :brandList="brandList" />
        <New :newGoodsList="newGoodsList">周一周四·新品首发</New>
        <Hot :hotGoodsList="hotGoodsList" />
        <Topic :topicList="topicList" />
        <New
          :newGoodsList="item.goodsList"
          v-for="item in categoryList"
          :key="item.id"
        >{{item.name}}</New>
      </div>
    </transition>

    <transition name="fade">
      <div class="popup_bg" v-if="$store.state.showpopup"></div>
    </transition>
    <transition name="slide">
      <keep-alive>
        <router-view v-if="$route.path=='/home/popup'"></router-view>
      </keep-alive>
    </transition>
  </div>
</template>

<script>
import Channel from "@/components/home/Channel";
import Brand from "@/components/home/Brand";
import New from "@/components/home/New";
import Hot from "@/components/home/Hot";
import Topic from "@/components/home/Topic";
import { GetHomeDataAPI } from "@/request/api";
export default {
  name: "Home",
  data() {
    return {
      value: "",
      banner: [],
      channel: [],
      brandList: [],
      newGoodsList: [],
      hotGoodsList: [],
      topicList: [],
      categoryList: [],
    };
  },
  components: {
    Channel,
    Brand,
    New,
    Hot,
    Topic,
  },
  created() {
    // 页面初始化时请求数据
    GetHomeDataAPI().then((res) => {
      // errno为0时，代表请求成功，这时候直接拿最里面的data
      console.log(res.data);
      if (res.errno == 0) {
        // console.log(res.data)
        let {
          banner,
          channel,
          brandList,
          newGoodsList,
          hotGoodsList,
          topicList,
          categoryList,
        } = res.data;
        this.banner = banner; // 获取到banner数组
        this.channel = channel; // 获取到channel数组
        this.brandList = brandList; // 获取到brandList数组
        this.newGoodsList = newGoodsList; // 获取到newGoodsList数组
        this.hotGoodsList = hotGoodsList; // 获取到hotGoodsList数组
        this.topicList = topicList; // 获取到topicList数组
        this.categoryList = categoryList; // 获取到categoryList数组
      }
    });
  },
  methods: {
    openPopup() {
      // 路由跳转，打开popup
      this.$router.push("/home/popup");
      this.$store.commit("changeShowpopup", true);
    },
  },
};
</script>


<style lang="less">
.home {
  padding-bottom: 60px;
}

.fade1-leave {
  opacity: 1;
}

.fade1-leave-active {
  transition: opacity 0.5s linear;
}

.fade1-leave-to {
  opacity: 0;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s linear;
}

.fade-enter-to,
.fade-leave {
  opacity: 1;
}

.popup_bg {
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  position: fixed;
  left: 0;
  top: 0;
  z-index: 998;
}
// 深度修改样式
/deep/.van-swipe__indicator {
  opacity: 1;
  background-color: skyblue;
}

/deep/.van-swipe__indicator--active {
  background-color: pink;
}

.home_title {
  height: 50px;
  line-height: 50px;
  text-align: center;
}
</style>